﻿using lingopen.Application;
using lingopen.Application.Jabil;
using lingopen.Entity.EPC;
using Microsoft.AspNetCore.Mvc;

namespace lingopen.Jabil.Server.Areas.BLE.Controllers
{
    /// <summary>
    /// EPC管理
    /// </summary>
    [Area(AreaNames.EPC)]
    [LoginAuthorize]
    public class BandingController : WebController
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pagination">页码</param>
        /// <param name="keyword">关键字</param>
        /// <param name="type">link5 link7 cnc5-1 cnc5-2 cnc6</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetModels(Pagination pagination, string keyword, string type)
        {
            PagedData<EPC_Banding> data = this.CreateService<IEPCAppService>().Query_EPC_Bandings(pagination, keyword, type);
            return this.UISuccessData(data);
        }
        [HttpGet]
        [SkipLoginAuthorizeAttribute]
        public string Get_Linked(string tool_code, string type)
        {
            var data = this.CreateService<IEPCAppService>().Get_Banding(tool_code, type);
            if (data != null)
                return data.Prod_Code;
            else return "";
        }
        [HttpPost]
        [SkipLoginAuthorizeAttribute]
        //[ValidateAntiForgeryToken]
        public ActionResult Add([FromBody]Add_EPC_Banding input, string type = "link5")
        {
            if (input.Prod_Code.IsNull()) return UIFailedData(input.Request_ID, "产品码不能为空");
            if (input.Tool_Code.IsNull()) return UIFailedData(input.Request_ID, "治具码不能为空");
            input.CreateUserId = this.CurrentSession == null ? "9f2ec0797d0f4fe290ab8b09a8302aba" : this.CurrentSession.UserId;
            try
            {
                EPC_Banding add = this.CreateService<IEPCAppService>().Banding(input, type);
                return this.UISuccessData(add.Request_ID);
            }
            catch (System.Exception ex)
            {
                return UIFailedData(input.Request_ID, ex.Message);
            }

        }
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Update(Update_EPC_Banding input)
        {
            this.CreateService<IEntityAppService>().UpdateFromDto<EPC_Banding, Update_EPC_Banding>(input);
            return this.UISuccessData();
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Delete(string id)
        {
            this.CreateService<IEntityAppService>().SoftDelete<EPC_Banding>(id, this.CurrentSession.UserId);
            return this.UISuccessData();
        }




    }
}
